/*
 * Copyright (c) 2017 <l_iupeiyu@qq.com> All rights reserved.
 */

package com.uyayo.portal.context.config;

import com.uyayo.core.resolver.CustomExceptionResolver;
import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;

import java.util.HashSet;
import java.util.Set;


@Configuration
public class ErrorPageConfig implements
        WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {

    @Bean
    public CustomExceptionResolver customExceptionResolver(){

        CustomExceptionResolver customExceptionResolver = new CustomExceptionResolver();
        customExceptionResolver.setExcludedExceptions(new Class[]{UnauthorizedException.class, UnauthenticatedException.class});
        return customExceptionResolver;
    }

    @Override
    public void customize(ConfigurableServletWebServerFactory factory) {
        Set<ErrorPage> set = new HashSet<>();
        set.add(new ErrorPage(HttpStatus.FORBIDDEN, "/403"));
        set.add(new ErrorPage(HttpStatus.NOT_FOUND, "/404"));
        set.add(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500"));
        set.add(new ErrorPage(Throwable.class,"/500"));

        factory.setErrorPages(set);
    }

    /*
    public void customize(ConfigurableEmbeddedServletContainer container) {
        container.addErrorPages(new ErrorPage(HttpStatus.FORBIDDEN, "/403"));
        container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/404"));
        container.addErrorPages(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500"));
        container.addErrorPages(new ErrorPage(Throwable.class,"/500"));
    }*/
}
